﻿namespace BadHabit.Factory.Data {
    
    
    public partial class TeamDataSet {

        public partial class MemberDataTable {


            public override void EndInit() {
                base.EndInit();
                TableNewRow += new System.Data.DataTableNewRowEventHandler(MemberDataTable_TableNewRow);
            }

            void MemberDataTable_TableNewRow(object sender, System.Data.DataTableNewRowEventArgs e) {
                SetDefaultValuesForRow((MemberRow)e.Row);
            }


            public void SetDefaultValuesForRow(MemberRow row) {
                row.MemberId = GetNextMemberId();
                row.NickName = "New Member";

                row.ClientContactRole = false;
                row.TeamRole = true;
                row.QualityAssuranceRole = false;
                row.ScrumMasterRole = false;
                row.ProductOwnerRole = false;
                row.CommercialGuyRole = false;
                row.FactoryOwnerRole = false;
                row.Company = "[DEFAULT COMPANY]";
                row.DefaultUnitName = "Dev";
            }

            private string GetNextMemberId() {
                int i = 1;
                string memberId = "DOMAIN\\Account";
                while (FindByMemberId(memberId) != null) {
                    memberId = "DOMAIN\\Account" + i;
                    i++;
                }
                return memberId;

            }
            
            protected override void OnColumnChanged(System.Data.DataColumnChangeEventArgs e) {
                //base.OnColumnChanged(e);
                MemberRow member = e.Row as MemberRow;

                // DO NOT REMOVE THIS LINE!!!                
                if (e.Row.RowState == System.Data.DataRowState.Deleted || e.Row.RowState == System.Data.DataRowState.Detached)
                    return;

                // if the Nick Name column was changed, sets the member Id for external members
                if (NickNameColumn.Equals(e.Column)) {
                    // if is not a external member
                    if (!member.ClientContactRole)
                        return;
                    member.MemberId = GenerateClientMemberId(member);
                }

                // if the Company column was changed, sets the member Id for external members
                if (CompanyColumn.Equals(e.Column)) {
                    // if is not a external member
                    if (!member.ClientContactRole)
                        return;

                    member.MemberId = GenerateClientMemberId(member);
                }
            }

            public string GenerateClientMemberId(MemberRow member) {
                string company = "[NO_COMPANY]";
                string nickName = "Client";

                if(!member.IsNull("Company"))
                    company = member.Company;
                if(!member.IsNull("NickName"))
                    nickName = member.NickName;

                string memberId = company + "\\" + nickName;

                int i = 1;
                while (FindByMemberId(memberId) != null) {
                    memberId = company + "\\" + nickName + i;
                    i++;
                }

                return memberId;
            }

        }
    }
}

